package service;

import entity.Manager;
import entity.Operator;
import function.Druid;
import function.ManagerDaoImpl;
import function.OperatorDaoImpl;
import interfaces.LogServiceDao;
import tool.MyTool;
import tool.TSUtility;
import view.LogView;

public class LogServiceDaoImpl implements LogServiceDao {

ManagerDaoImpl managerDao=ManagerDaoImpl.getManagerDao();
OperatorDaoImpl operatorDao=OperatorDaoImpl.getOperatorDao();
OperatorServiceDaoImpl operatorServiceDao=OperatorServiceDaoImpl.getOperatorService();
ManagerServiceImpl managerService=ManagerServiceImpl.getManagerService();
private static LogServiceDaoImpl logServiceDao=null;
private LogServiceDaoImpl(){}
public static LogServiceDaoImpl getLogServiceDao(){
    if(logServiceDao==null)
        logServiceDao=new LogServiceDaoImpl();
    return logServiceDao;
}
    @Override
    public Manager checkInManger() throws Exception {
        System.out.println("-----管理员登录-----");
        System.out.println("请输入用户名：（直接按确定退出）");
        String name = MyTool.getSixString(true, "");
        if(name.equals("")){
            System.out.println("退出成功");
            return null;
        }
        System.out.println("请输入密码：");
        String password = MyTool.getSixString(false, null);
        Manager manager = managerDao.selectByName(Druid.getConnection(), name);
        if (manager==null) {
            System.out.println("账户不存在，是否注册（Y/N）：");
            char c = TSUtility.readConfirmSelection();
            if(c=='N'){
                return null;
            }
            registerManager();
            return null;
        }
        else if (manager.getName().equals(name)&&manager.getPassword().equals(password))
        {
            Journal.record(name,"登录了图书管理系统");
            System.out.println("登录成功，欢迎您用户"+manager.getName());
            return manager;
        }
        System.out.println("登录失败，账号或者密码错误");
        return null;
    }

    @Override
    public Operator checkInOperator() throws Exception {
        System.out.println("-----操作员登录-----");
        System.out.println("请输入用户名：（直接按确定退出）");
        String name = MyTool.getSixString(true, "");
        if(name.equals("")){
            System.out.println("退出成功过");
            return null;
        }
        System.out.println("请输入密码：");
        String password = MyTool.getSixString(false, null);
        Operator operator = operatorDao.SelectByName(Druid.getConnection(), name);
        if(operator==null){
            System.out.println("账户不存在，是否注册（Y/N）：");
            char c = TSUtility.readConfirmSelection();
            if(c=='N'){
                return null;
            }
            operatorServiceDao.insert();
            return null;
        }
         else if (operator.getName().equals(name)&&operator.getPassword().equals(password)){
            Journal.record(name,"登录了图书管理系统");
            System.out.println("登录成功,欢迎你"+operator.getName());
            return operator;
        }
            System.out.println("账号或密码错误,请重新输入");
        return null;
    }

    @Override
    public void registerManager() throws Exception {
        System.out.println("-----管理员注册-----");
        managerService.insert();
    }

    @Override
    public void modifyPassword() throws Exception {
        System.out.println("-----管理员修改-----");
        managerService.update();
    }

    @Override
    public void foundPassword() throws Exception {
        System.out.println("-----找回密码-----");
        System.out.println("请输入你的账户名(直接按确定退出)：");
        String name = MyTool.getSixString(true, "");
        if(name.equals("")){
            System.out.println("退出成功");
            return;
        }
        Manager manager = managerDao.selectByName(Druid.getConnection(), name);
        if(manager!=null)
        System.out.println("你的账户信息如下："+manager.toString());
        else System.out.println("该账户不存在");
    }

    public static void main(String[] args) {
        LogServiceDaoImpl logServiceDao=new LogServiceDaoImpl();
        try {
            logServiceDao.registerManager();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
